package pers.qianyu.month_202102.date_20210216;

/**
 * 1143. 最长公共子序列
 * https://leetcode-cn.com/problems/longest-common-subsequence/
 * - 状态压缩
 *
 * @author mizzle rain
 * @date 2021年2月16日21:10:34
 */
public class LongestCommonSubsequence2 {
    public int longestCommonSubsequence(String text1, String text2) {
        int m = text1.length(), n = text2.length();
        if (m == 0 || n == 0) return 0;
        int[] dp = new int[n + 1];
        for (int i = 1; i <= m; i++) {
            int pre = 0;
            for (int j = 1; j <= n; j++) {
                int t = dp[j];
                if (text1.charAt(i - 1) == text2.charAt(j - 1)) dp[j] = pre + 1;
                else dp[j] = Math.max(dp[j], dp[j - 1]);
                pre = t;
            }
        }
        return dp[n];
    }
}